package com.yonyou.pmclouds.outter.role;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.iuap.tenant.sdk.UserRoleCenter;
import com.yonyou.pmclouds.basecom.conf.DeployTypeConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class UserRoleInfoQuery {

    private ObjectMapper jsonMapper = new ObjectMapper();

    /**
     * 获得用户所拥有的角色
     * @param pkUser
     * @param tenantId
     * @return
     */
    public String[] getUserRoles(String pkUser, String tenantId){
        String auth = UserRoleCenter.queryRoleByUser(pkUser, tenantId, DeployTypeConfiguration.getSysId());
        if(StringUtils.isEmpty(auth)){
            log.error("查询用户角色失败：友户通服务异常");
            return new String[0];
        }
        Map<String, Object> roleMap = new HashMap<String,Object>();
        try {
            roleMap = jsonMapper.readValue(auth, Map.class);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        if(roleMap == null)
            return new String[0];

        List<String> roleList = (List) roleMap.get("data");
        Set<String> pkRoleSet = new HashSet<>();
        pkRoleSet.addAll(roleList);
        return pkRoleSet.toArray(new String[0]);
    }


}
